package frontend.user.product.controller;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import backend.attribute.manager.AttributeSetManager;
import backend.product.manager.ProductAttributeManager;
import backend.product.manager.ProductAttributeSetManager;
import backend.product.manager.ProductManager;

import com.opensymphony.xwork2.ActionSupport;

import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.attribute.model.AttributeValue;
import core.product.model.Product;
import core.product.model.ProductAv;
import core.product.model.ProductData;
import core.product.model.ProductType;

public class UserProductAttributeController extends ActionSupport implements ServletRequestAware
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private ProductManager pm ;
	private AttributeSetManager asm ;
	private ProductAttributeSetManager pasm ;
	private String sku ;
	private Long prodOwnerId ;
	
	public UserProductAttributeController()
	{
		pm = new ProductManager(); 
		asm = new AttributeSetManager();
		pasm = new ProductAttributeSetManager();
	}
	public String updateAttractions()
	{
		String productId = request.getParameter("productId");
		Product p = pm.getProductByUuid(Long.parseLong(productId));
		String attributeSetName = request.getParameter("attributeSetName");
		AttributeSet as = asm.getAttributeSetByName(attributeSetName);
		Iterator<ProductAv> pavLstItr = pasm.getProductAvByAttributeSetAndProduct(p, as).iterator();
		while(pavLstItr.hasNext())
		{
			ProductAv pav = pavLstItr.next();
			pav.setAttributeValues(null);
			Attribute a = pav.getAttribute();
			Set<AttributeValue> newAvsSet = new HashSet<AttributeValue>();
			if(a.isMultivalued())
			{
				Iterator<AttributeValue> valItr = a.getValues().iterator();
				while(valItr.hasNext())
				{
					AttributeValue av = valItr.next();
					if(av.getAttributeType().equals("checkbox"))
					{
						String k = "chk_"+av.getUuid() ;
						if(this.request.getParameter(k.trim()) != null)
						{
							String value = this.request.getParameter(k.trim()) ;
							if(value.equals("true"))
							{
								newAvsSet.add(av);
							}
						}
					}
				}
				pav.setAttributeValues(newAvsSet);
				pasm.save(pav);
			}
		}
		return SUCCESS ;
	}
	public String updateProductData()
	{
		//getting product
		String productId = request.getParameter("productId");
		Product p = pm.getProductByUuid(Long.parseLong(productId));
		// getting product description data.....
		String locationDescription = request.getParameter("locationDescription");
		String otherActivities = request.getParameter("otherActivities");
		String skiInTheArea = request.getParameter("skiInTheArea");
		String golfInTheArea = request.getParameter("golfInTheArea");
		ProductData pd = p.getProductData();
		if(pd == null)
		{
			pd = new ProductData(); 
		}
		pd.setLocationDescription(locationDescription);
		pd.setOtherActivities(otherActivities);
		pd.setSkiInTheArea(skiInTheArea);
		pd.setGolfInTheArea(golfInTheArea);
		p.setProductData(pd);
		pm.saveProduct(p);
		return SUCCESS;
	}
	public String updateProductType()
	{
		String productId = request.getParameter("productId");
		Product p = pm.getProductByUuid(Long.parseLong(productId));
		// reset all product type
		p.setProductType(null);
		Iterator<ProductType> ptItr = pm.getgetAllProductType().iterator();
		
		Set<ProductType> newSet = new HashSet<ProductType>();
		while(ptItr.hasNext())
		{
			ProductType pt = ptItr.next();
			String name = "producttype_"+pt.getUuid();
			if(request.getParameter(name) != null)
			{
				String value = request.getParameter(name);
				if(value.equals("true"))
				{
					newSet.add(pt);
				}
			}
		}
		p.setProductType(newSet);
		pm.saveProduct(p);
		return SUCCESS ;
	}
	public String updateGeneralAttribute()
	{//productId=1&Car=necessary&Car=not necessary&Car=recommended&Nearest Airport=a&Nearest Airport Distance=a&Nearest Airport Distance Unit=miles&Nearest Beach=a&Nearest Beach Distance=a&Nearest Beach Distance Unit=miles&Nearest Ferry=a&Nearest Ferry Distance=a&Nearest Ferry Distance Unit=miles&Nearest Train=a&Nearest Train Distance=a&Nearest Train Distance Unit=miles&Nearest Highway=a&Nearest Highway Distance=a&Nearest Highway Distance Unit=miles&Nearest Bar=a&Nearest Bar Distance=a&Nearest Bar Distance Unit=miles&Nearest Ski=a&Nearest Ski Distance=a&Nearest Ski Distance Unit=miles&Nearest Golf=a&Nearest Golf Distance=a&Nearest Golf Distance Unit=miles&Nearest Restaurant=a&Nearest Restaurant Distance=a&Nearest Restaurant Distance Unit=miles&save=Save&mycar=not necessary
		String productId = request.getParameter("productId");
		String attributeSet = request.getParameter("attributeSet");
		Product p = pm.getProductByUuid(Long.parseLong(productId));
		AttributeSet as = asm.getAttributeSetByName(attributeSet);
		Iterator<Attribute> attrItr = as.getAttributes().iterator();
		
		while(attrItr.hasNext())
		{
			Attribute a = attrItr.next();
			String name = a.getName();
			if(request.getParameter(name) != null)
			{
				
				String value = request.getParameter(name) ;
				if(name.equals("Car"))
				{
					 value = request.getParameter("mycar") ;
				}
				ProductAv pav = pasm.getProductAvByAttributeSetAndProductAndAttribute(p, as, a);
				if(pav == null)
				{
					pav = new ProductAv();
					pav.setProduct(p);
					pav.setAttributeSet(as);
					pav.setAttribute(a);
				}
				pav.setValue(value);
				pasm.save(pav);
			}
		}
		return SUCCESS ;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request ;
	}
	/**
	 * @return the sku
	 */
	public String getSku() {
		return sku;
	}
	/**
	 * @param sku the sku to set
	 */
	public void setSku(String sku) {
		this.sku = sku;
	}
	/**
	 * @return the prodOwnerId
	 */
	public Long getProdOwnerId() {
		return prodOwnerId;
	}
	/**
	 * @param prodOwnerId the prodOwnerId to set
	 */
	public void setProdOwnerId(Long prodOwnerId) {
		this.prodOwnerId = prodOwnerId;
	}
}
